/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function( $, common, ready, Collection, template, Model, dialog ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'coupons',
                condition: {status: 'all'},
                model: {
                    url:_global.url.api + 'coupon',
                    event: {
                        '.item-online:click': 'online',
                        '.item-offline:click': 'offline',
                        '.item-creat_code:click':'create_code'
                    },
                    online: function(e) {
                        e.preventDefault();
                        var _this = this;
                        $.ajax({
                            url: _global.url.api + 'coupon?is_open=1&id=' + this.get_data('id'),
                            type: 'put',
                            success: function(res) {
                                if(res.code == 0) {
                                    _this.data.is_open = 1;
                                    _this.create_element();
                                }
                            },
                            error: function() {
                                Alert({msg: '通讯错误', type: 'error'});
                            }
                        });
                    },
                    offline: function(e) {
                        e.preventDefault();
                        var _this = this;
                        $.ajax({
                            url: _global.url.api + 'coupon?is_open=0&id=' + this.get_data('id'),
                            type: 'put',
                            success: function(res) {
                                if(res.code == 0) {
                                    _this.data.is_open = 0;
                                    _this.create_element();
                                }
                            },
                            error: function() {
                                Alert({msg: '通讯错误', type: 'error'});
                            }
                        });
                    },create_code:function(e){
                        e.preventDefault();
                        var id =this.data.id;
                        var model =this;
                        if(new Model({
                                url: _global.url.api + 'coupop_code',
                                data:{id:id}
                            }).put()) {
                            _this.collection.reload();
                        };
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //查询
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });

            /**
             * 按状态查询
             */
            $('#btn-status').find('a').on('click', function (e) {
                e.preventDefault();
                $('input[name="keyword"]').val('');
                _this.collection.condition.keyword = '';
                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });

        }
    };

    new index();

});